﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Services
{
    public class MaterialTypeService
    {
        public List<tbl_material_type> GetMaterialType(int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                count = db.tbl_material_types.Count(u => u.material_type_name.Contains(keyword));

                return db.tbl_material_types.Where(u => u.material_type_name.Contains(keyword)).OrderBy(sortColumn + direction).ToList();
            }
        }

        public bool UpdateMaterialType(tbl_material_type model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    tbl_material_type eModel;

                    if (model.material_type_id == 0)
                    {
                        model.material_type_create_by = PavingContext.CurrentUserId();
                        model.material_type_create_date = DateTime.Now;

                        db.tbl_material_types.InsertOnSubmit(model);
                    }
                    else
                    {
                        eModel = db.tbl_material_types.SingleOrDefault(s => s.material_type_id.Equals(model.material_type_id));

                        if (eModel != null)
                        {
                            eModel.material_type_name = model.material_type_name;
                            eModel.material_type_unit = model.material_type_unit;

                            eModel.material_type_update_by = PavingContext.CurrentUserId(); ;
                            eModel.material_type_update_date = DateTime.Now;
                        }
                    }

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(tbl_material_type model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    var eModel = db.tbl_material_types.SingleOrDefault(s => s.material_type_id.Equals(model.material_type_id));

                    if (eModel != null)
                    {
                        db.tbl_material_types.DeleteOnSubmit(eModel);
                        db.SubmitChanges();

                        return true;
                    }

                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}